package com.controller;

import com.pojo.Provider;
import com.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @ResponseBody
    @RequestMapping("/providerList.do")
    public List<Provider> findAllProviderController(){
        List<Provider> list = providerService.findAll();
        return list;
    }

    @ResponseBody
    @RequestMapping("/providerAdd.do")
    public boolean addProviderController(@RequestBody Provider provider){
        boolean isok = providerService.doAddProviderService(provider);
        return isok;
    }

    @RequestMapping("/providerView.do")
    public String viewProviderController(Long id,HttpSession session){
        Provider provider = providerService.findProviderById(id.intValue());
        session.setAttribute("provider",provider);
        return "providerView";
    }

    @RequestMapping("/providerSearch.do")
    public String searchProviderController(String proName,HttpSession session){
        System.out.println(proName);
        if (proName==null||proName==""){
            return "redirect:providerList.do";
        }
        List<Provider> list = providerService.findSearchProvider(proName);
        session.setAttribute("proList",list);
        return "providerList";
    }

    @ResponseBody
    @RequestMapping("/providerDelete.do")
    public boolean deleteProviderController(String id){
        boolean isok = providerService.doDeleteProviderService(Integer.valueOf(id));
        return isok;
    }

    @ResponseBody
    @RequestMapping("/providerUpdateView.do")
    public Provider updateViewProviderController(Long id){
        Provider provider = providerService.findProviderById(id.intValue());
        return provider;
    }

    @ResponseBody
    @RequestMapping("/providerUpdate.do")
    public boolean updateProviderController(@RequestBody Provider provider){
        boolean isok = providerService.doUpdateProviderService(provider);
        return isok;
    }

}
